/*
 * Name: Chao Lai
 * Date: 1/23/13
 * Program: Lab 1
 * File: Window.java
 * Purpose: Create a window that implements Java Swing
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Window extends JPanel implements ActionListener 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static int width;
	private static int height;
	private JLabel label;
	private JMenu m1;

	public Window() 
	{
		super(new BorderLayout());
		
		// Label
		label = new JLabel("Lab 1", JLabel.CENTER);
		label.setToolTipText("Testing");
		label.setBorder(BorderFactory.createLineBorder(Color.black));

		// Button 1
		JButton b1 = new JButton(
				"<html><center><b><u>E</u>nable <u>M</u>enu</b><br></center></html>");
		b1.setEnabled(true);
		
		b1.setActionCommand("b1");
		b1.addActionListener(this);
		
		add(label, BorderLayout.CENTER);
		add(b1, BorderLayout.NORTH);
	}

	public JMenuBar createMenu() 
	{
		// Menu bar
		JMenuBar menu = new JMenuBar();
		menu.setPreferredSize(new Dimension(width, 20));

		// Menu 1
		this.m1 = new JMenu("Change JLabel Text");
		menu.add(m1);

		// Menu 1 Items
		JMenuItem menuItem = new JMenuItem("Change 1");
		menuItem.setActionCommand("c1");
		m1.add(menuItem);

		JMenuItem menuItem2 = new JMenuItem("Change 2");
		menuItem2.setActionCommand("c2");
		m1.add(menuItem2);

		//
		m1.addSeparator();
		//
		
		JMenuItem menuItem3 = new JMenuItem("Change 3");
		menuItem3.setActionCommand("c3");
		m1.add(menuItem3);

		m1.setEnabled(false);
		
		
		// Action Listeners
		menuItem.addActionListener(this);
		menuItem2.addActionListener(this);
		menuItem3.addActionListener(this);
		
		return menu;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if ("c1".equals(e.getActionCommand())) 
		{
			label.setText("Change 1");
        }
		else if ("c2".equals(e.getActionCommand())) 
		{
			label.setText("Change 2");
        }
		else if ("c3".equals(e.getActionCommand())) 
		{
			label.setText("Change 3");
		}
		else if ("b1".equals(e.getActionCommand())) 
		{
			m1.setEnabled(true);
		}
	}

	public static void createGUI() 
	{
		width = 400;
		height = 400;

		JFrame frame = new JFrame();

		Window win = new Window();
		
		// Add components to JFrame
		frame.setJMenuBar(win.createMenu());
		frame.setContentPane(win);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width, height);
		frame.setLocationRelativeTo(null);
		frame.setTitle("Lab 1");
		frame.setVisible(true);
	}

	public static void main(String[] args) 
	{
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() 
		{
			public void run() 
			{
				createGUI();
			}
		});
	}
}
